package com.itboye.textileexpes.repository;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.textileexpes.beans.NewsKefuBeans;
import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.interfaces.IUser;
import com.itboye.textileexpes.json.ByJsonRequest.Builder;
import com.itboye.textileexpres.base.BaseErrorListener;
import com.itboye.textileexpres.base.BaseNetRepository;
import com.itboye.textileexpres.base.BaseSuccessReqListener;

public class UserReposit extends BaseNetRepository implements IUser<UserBean> {
	public static String Login = "By_User_login";
	public static String Register = "By_User_register";
	String API = "100";
	public static String LoginByVer = "by_SecurityCode_send";// 通过验证码登录
	public static String FindPwdByPhone = "By_User_updatePwd";// 找回密码（通过手机号码找回）
	public static String UpdatePwdByPwd = "By_User_updatePwdByOldPwd";// 找回密码（通过原密码找回）
	public static String UpdateUserInfo = "By_User_update";// 修改用户信息
	public static String LoginByCode = "By_User_loginByCode";// 短信验证登录
	public static String AutoLogin = "By_User_autoLogin";// 自动登录
	

	public UserReposit(ICompleteListener listener) {
		// TODO Auto-generated constructor stub
		this.setListener(listener);

	}

	public UserReposit() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(UserBean model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(UserBean model) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(UserBean model) {
		// TODO Auto-generated method stub
		String apiVer = "100";
		// Type type = new TypeToken<WXBindModel>() {
		// }.getType();
		Map<String, Object> params = new HashMap<String, Object>();
		switch (model.getType()) {
		case 1:
			params.put("mobile", model.getUsername());
			params.put("password", model.getPassword());
			params.put("code", model.getCode());
			params.put("country", model.getCountryNo());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(FindPwdByPhone, "101",
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
			break;
		case 2:
			params.put("uid", model.getUid());
			params.put("nickname", model.getNickname());
			params.put("sex", model.getSex());
			params.put("sign", model.getSign());
			params.put("email", model.getEmail());
			params.put("weixin", model.getWxopenid());
			params.put("company", model.getCompany());
			params.put("job_title", model.getJobTitle());
			params.put("loc_country", model.getLocCountry());
			params.put("loc_area", model.getLocArea());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(UpdateUserInfo, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();
			break;
		// case 3:// 通过手机号码更换绑定手机
		// params.put("uid", model.getUid());
		// params.put("password", model.getPassword());
		// params.put("mobile", model.getMobile());
		// params.put("code", model.getYanzhengma());
		// (new Builder<WXBindModel>())
		// .setTypeVerParamsAndReturnClass(UpdateBindPhoneNumber,
		// apiVer, params, type)
		// .requestListener(
		// new BaseSuccessReqListener<WXBindModel>(
		// getListener()))
		// .errorListener(new BaseErrorListener(getListener()))
		// .desEncodeThenBuildAndSend();
		// case 4:// 第一次绑定手机号码
		// params.put("uid", model.getUid());
		// params.put("code", model.getYanzhengma());
		// params.put("mobile", model.getMobile());
		// params.put("password", model.getMobile());
		// (new Builder<WXBindModel>())
		// .setTypeVerParamsAndReturnClass(BindPhoneNumber, apiVer,
		// params, type)
		// .requestListener(
		// new BaseSuccessReqListener<WXBindModel>(
		// getListener()))
		// .errorListener(new BaseErrorListener(getListener()))
		// .desEncodeThenBuildAndSend();
		// break;
		case 5:// 通过密码修改密码
			params.put("uid", model.getUid());
			params.put("password", model.getPassword());
			params.put("new_password", model.getNewpassword());
			(new Builder<String>())
					.setTypeVerParamsAndReturnClass(UpdatePwdByPwd, apiVer,
							params, String.class)
					.requestListener(
							new BaseSuccessReqListener<String>(getListener()))
					.errorListener(new BaseErrorListener(getListener()))
					.desEncodeThenBuildAndSend();

		default:
			break;
		}
	}

	@Override
	public void getInfo(UserBean model) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<UserBean> query(UserBean model) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void login(UserBean model) {
		// TODO Auto-generated method stub
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		Type type = new TypeToken<UserBean>() {
		}.getType();
		// 账号密码登录
		params.put("username", model.getUsername());
		params.put("password", model.getPassword());
		params.put("country", model.getCountryNo());
		// 目前最简化调用
		// START
		(new Builder<UserBean>())
				.setTypeVerParamsAndReturnClass(Login, apiVer, params,
						UserBean.class)
				.requestListener(
						new BaseSuccessReqListener<UserBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void register(UserBean model) {
		// TODO Auto-generated method stub
		String apiVer = "101";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("code", model.getCode());
		params.put("country", model.getCountryNo());
		params.put("username", model.getUsername());
		params.put("password", model.getPassword());
		params.put("from", model.getRegFrom());
		
		// 目前最简化调用
		// START
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(Register, apiVer, params,
						String.class)
				.requestListener(
						new BaseSuccessReqListener<String>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void onLoginByCode(String country, String mobile, String code) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<UserBean>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("country", country);
		params.put("mobile", mobile);
		params.put("code", code);
		// 目前最简化调用
		// START
		(new Builder<UserBean>())
				.setTypeVerParamsAndReturnClass(LoginByCode, apiVer, params,
						type)
				.requestListener(
						new BaseSuccessReqListener<UserBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	@Override
	public void Autologin(UserBean model) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<UserBean>() {
		}.getType();
		String apiVer = "100";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("auto_login_code", model.getAutoLoginCode());
		params.put("uid", model.getUid());
		// 目前最简化调用
		// START
		(new Builder<UserBean>())
				.setTypeVerParamsAndReturnClass(AutoLogin, apiVer, params, type)
				.requestListener(
						new BaseSuccessReqListener<UserBean>(getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}

	
}
